from odoo import api, fields, models


class ResPartner(models.Model):
    _inherit = 'res.partner'
    s_document_ids = fields.One2many('documents.document', 'res_id', string='Documents',
                                     domain="[('res_model', '=', 'res.partner'), ('res_id', '=', id)]")

    @api.model
    def name_search(self, name='', args=None, operator='ilike', limit=100):
        if self._context.get('ship_instructions'):
            ids = self._name_search(name, args, operator, limit=limit, order=self._order)
            records = self.browse(ids)
            display_names = [self._get_address(rec) for rec in records]
        else:
            display_names = super().name_search(name, args, operator, limit)
        return display_names

    @api.depends('name', 'code', 'country_id', 'state_id', 'city', 'street2', 'street')
    def _compute_display_name(self):
        if self._context.get('ship_instructions'):
            for obj in self:
                obj.display_name = self._get_address(obj)[1]
        else:
            return super()._compute_display_name()

    def _get_address(self, obj):
        country = obj.country_id.name + ' ' if obj.country_id.name else ''
        state = obj.state_id.name + ' ' if obj.state_id.name else ''
        city = obj.city + ' ' if obj.city else ''
        financial_street = obj.street + ' ' if obj.street else ''
        financial_street2 = obj.street2 + ' ' if obj.street2 else ''
        delivery_address = country + state + city + financial_street + '\n' + financial_street2
        return (obj.id, delivery_address)

    def action_open_document_upload_wizard(self):
        self.ensure_one()
        return {
            'type': 'ir.actions.act_window',
            'name': 'Upload Document',
            'res_model': 'document.upload.wizard',
            'view_mode': 'form',
            'target': 'new',
            'context': {
                'default_res_model': 'res.partner',
                'default_res_id': self.id,
                'default_folder_id': self.with_context(lang='en_US').env['documents.folder'].search(
                    [('name', '=', 'Customer')]).id,
                'default_partner_id': self.id,
                'default_tag_id': self.with_context(lang='en_US').env['documents.tag'].search(
                    [('name', '=', 'Customer Agreement')]).id
            },
        }

    def action_view_documents(self):
        self.ensure_one()
        return {
            'name': 'Documents',
            'type': 'ir.actions.act_window',
            'res_model': 'documents.document',
            'view_mode': 'kanban,tree,form',
            'domain': [
                ('res_model', '=', 'res.partner'),
                ('res_id', '=', self.id),
            ],
            'context': {
                'default_res_model': 'res.partner',
                'default_res_id': self.id,
                'create': False,
                'searchpanel_default_folder_id': self.with_context(lang='en_US').env['documents.folder'].search(
                    [('name', '=', 'Customer')]).id,
            },
            'target': 'current',
        }
